package plots;

import java.awt.Color;

import processing.core.PApplet;
import util.PickeableObject;
import util.VAPoint;

/**
 * 
 * @author Juan Camilo Ibarra
 *
 */
public class ParallelCoordinatesPlotLine extends PickeableObject{

	private int[][] controlPoints;
	private VAPoint[] visualPoints;
	private int baseColor;
	private int selectedColor;
	
	private boolean flag;
	
	public ParallelCoordinatesPlotLine(PApplet mainApplet, int[][] points)
	{
		super(mainApplet);
		this.controlPoints = points;
		this.visualPoints = new VAPoint[controlPoints.length];
		this.baseColor = Color.WHITE.getRGB();
		this.selectedColor = Color.YELLOW.getRGB();
		this.flag = false;
		
		for(int i = 0; i < controlPoints.length; i++)
		{
			visualPoints[i] = new VAPoint(controlPoints[i][0], controlPoints[i][1], mainApplet);
			visualPoints[i].setId(i);
			visualPoints[i].setColor(baseColor);
		}
	}
	
	public void setBaseColor(int baseColor) {
		this.baseColor = baseColor;
	}

	public void setSelectedColor(int selectedColor) {
		this.selectedColor = selectedColor;
	}

	public void setCurrentColor(int currentColor)
	{
		this.selectedColor = currentColor;
	}
	
	public boolean mouseIsOver()
	{
		boolean noneSelected = true;
		boolean mouseIsOver = false;
		for(VAPoint p : visualPoints)
		{
			if(p.mouseIsOver())
			{
				mouseIsOver = true;
				noneSelected = false;
				break;
			}
		}
		if(noneSelected)
		{
			mouseIsOver = false;
		}
		return mouseIsOver;
	}
	
	public void setChanged()
	{
		super.setChanged();
		flag = true;
	}
	
	public void drawLine()
	{
		if(changed)
		{
//			System.out.println("drawing line");
			int startX = controlPoints[0][0];
			int startY = controlPoints[0][1];
			int endX = 0;
			int endY = 0;
			for(VAPoint p : visualPoints)
			{
				if(isSelected())
				{
					p.setColor(selectedColor);
					p.setSelected();
				}
				else
				{
					p.setColor(baseColor);
					p.setUnselected();
				}
				if(flag)
				{
					p.setChanged();
				}
				p.drawPoint();
			}
			
			for(int i = 1; i < controlPoints.length; i++)
			{
				endX = controlPoints[i][0];
				endY = controlPoints[i][1];
				if(isSelected())
				{
					mainApplet.stroke(selectedColor);
					mainApplet.strokeWeight(4);
				}
				else
				{
					mainApplet.stroke(baseColor);
					mainApplet.strokeWeight(1);
				}
				mainApplet.line(startX, startY, endX, endY);
				startX = endX;
				startY = endY;
			}
			mainApplet.stroke(baseColor);
			changed = false;
		}
	}
}
